{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "AugLy_image.ipynb",
      "provenance": [],
      "collapsed_sections": []
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "code",
      "metadata": {
        "id": "jfd6YTPgRFdG"
      },
      "source": [
        "# Note: restart runtime after this import before running the augmentations\n",
        "!pip install -U augly[image]\n",
        "!sudo apt-get install python3-magic"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "OQwQz89pRIB_"
      },
      "source": [
        "import os\n",
        "import augly.image as imaugs\n",
        "import augly.utils as utils\n",
        "from IPython.display import display\n",
        "\n",
        "# Get input image, scale it down to avoid taking up the whole screen\n",
        "input_img_path = os.path.join(\n",
        "    utils.TEST_URI, \"image\", \"inputs\", \"dfdc_1.jpg\"\n",
        ")\n",
        "\n",
        "# We can use the AugLy scale augmentation\n",
        "input_img = imaugs.scale(input_img_path, factor=0.2)\n",
        "display(input_img)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "PmqH2_N3TEp7"
      },
      "source": [
        "# Now we can apply various augmentations to the scaled image!\n",
        "display(\n",
        "    imaugs.meme_format(\n",
        "        input_img,\n",
        "        caption_height=75,\n",
        "        meme_bg_color=(0, 0, 0),\n",
        "        text_color=(255, 255, 255),\n",
        "    )\n",
        ")"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "0R2CFQTwmqcd"
      },
      "source": [
        "\"\"\"\n",
        "You can optionally pass in a metadata list, to which metadata about the\n",
        "augmentation will be appended, including kwargs, input & output\n",
        "dimensions, and intensity (defined based on the kwargs for each\n",
        "augmentation).\n",
        "\"\"\"\n",
        "meta = []\n",
        "display(imaugs.shuffle_pixels(input_img, factor=0.3, metadata=meta))\n",
        "meta"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "edu7vU1245vw"
      },
      "source": [
        "\"\"\"\n",
        "You can also pass in bounding boxes, which will be transformed along with\n",
        "the image & included in the metadata (note: you must provide metadata to\n",
        "get the transformed bboxes)\n",
        "\"\"\"\n",
        "meta = []\n",
        "display(\n",
        "    imaugs.rotate(\n",
        "        input_img,\n",
        "        degrees=15,\n",
        "        metadata=meta,\n",
        "        bboxes=[(20, 6, 250, 180)],\n",
        "        bbox_format=\"pascal_voc\",\n",
        "    )\n",
        ")\n",
        "meta"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "vxfJ1QwaZ9FO"
      },
      "source": [
        "# For all the augmentations, we have class-based definitions as well as\n",
        "# functional\n",
        "meta = []\n",
        "aug = imaugs.PerspectiveTransform(sigma=20.0)\n",
        "display(aug(input_img, metadata=meta))\n",
        "meta"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "WKX3Ox4ZWbBa"
      },
      "source": [
        "\"\"\"\n",
        "For some augmentations, we also provide versions that will randomly sample\n",
        "from a set of parameters (e.g. for ChangeAspectRatio, RandomAspectRatio\n",
        "samples an emoji from Twitter's Twemoji set which we provide in the augly\n",
        "package). The metadata will contain the actual sampled param values.\n",
        "\"\"\"\n",
        "meta = []\n",
        "aug = imaugs.RandomAspectRatio()\n",
        "display(aug(input_img, metadata=meta))\n",
        "meta"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "LwkB_Rz4TMz2"
      },
      "source": [
        "# You can also compose several transformations together\n",
        "meta = []\n",
        "aug = imaugs.Compose(\n",
        "    [\n",
        "        imaugs.Saturation(factor=2.0),\n",
        "        imaugs.OverlayOntoScreenshot(\n",
        "            template_filepath=os.path.join(\n",
        "                utils.SCREENSHOT_TEMPLATES_DIR, \"mobile.png\"\n",
        "            ),\n",
        "        ),\n",
        "        imaugs.Scale(factor=0.6),\n",
        "    ]\n",
        ")\n",
        "display(aug(input_img, metadata=meta))\n",
        "meta"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "YSMPLj7sfbiY"
      },
      "source": [
        "# AugLy also integrates seamlessly with PyTorch transforms\n",
        "# Note: you must have torchvision installed, which it is by default in colab\n",
        "import torchvision.transforms as transforms\n",
        "\n",
        "aug = transforms.Compose(\n",
        "    [\n",
        "        imaugs.Brightness(factor=2.0),\n",
        "        imaugs.RandomRotation(),\n",
        "        transforms.ToTensor(),\n",
        "    ]\n",
        ")\n",
        "type(aug(input_img))"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "059GZAl6kjeq"
      },
      "source": [
        "# We also provide a numpy wrapper in case your data is in np.ndarray format\n",
        "import numpy as np\n",
        "from augly.image import aug_np_wrapper, overlay_emoji\n",
        "\n",
        "np_image = np.zeros((300, 300))\n",
        "# pass in function arguments as kwargs\n",
        "np_aug_img = aug_np_wrapper(np_image, overlay_emoji, **{'opacity': 0.5, 'y_pos': 0.45})\n",
        "type(np_aug_img)"
      ],
      "execution_count": null,
      "outputs": []
    }
  ]
}